package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.seller.TbSpecificationMapper;
import com.dongdongshop.mapper.seller.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.Vo.SpecificationOptionVo;
import com.dongdongshop.pojo.seller.TbSpecification;
import com.dongdongshop.pojo.seller.TbSpecificationExample;
import com.dongdongshop.pojo.seller.TbSpecificationOption;
import com.dongdongshop.pojo.seller.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecifiCationService;
import com.dongdongshop.util.PageBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Service
@Component
public class SpecifiCationServiceImpl implements SpecifiCationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;
    @Override
    public PageBean<TbSpecification> findSpecifiAll(String specName,Integer pageNumber, Integer pageSize) {
        PageBean<TbSpecification> page = new PageBean<>();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);

        List<TbSpecification> list = specificationMapper.findSpecifiAll(specName,page.getStartIndex(),pageSize);
        page.setResult(list);

        Integer count = specificationMapper.getcount();
        page.setTotalCount(count);
        return page;
    }

    @Override
    public List<TbSpecification> getspecifiAll() {
        List<TbSpecification> list = specificationMapper.selectByExample(new TbSpecificationExample());
        return list;
    }

    @Transactional
    @Override
    public void addSpecifiByName(TbSpecification specification,Integer[] orders,String[] optionName) {

        specificationMapper.addSpecifiByName(specification);
        Long id = specification.getId();
        System.out.println(id);
        List<TbSpecificationOption> list = new ArrayList<>();
        for (int i = 0; i < orders.length; i++){
            TbSpecificationOption specificationOption = new TbSpecificationOption();
            specificationOption.setOptionName(optionName[i]);
            specificationOption.setSpecId(id);
            specificationOption.setOrders(orders[i]);
            list.add(specificationOption);
        }
        specificationOptionMapper.insertInfo(list);
    }

    @Transactional
    @Override
    public int deleteInfo(Integer[] ids) {

        specificationOptionMapper.deleteInfo(ids);
        return specificationMapper.deleteInfo(ids);
    }

    @Override
    public List<SpecificationOptionVo> selectById(Long id) {

        List<SpecificationOptionVo> list = specificationMapper.selecById(id);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateInfo(TbSpecification specification, String[] optionName, Integer[] orders) {
        //先修改主表的数据
        specificationMapper.updateByPrimaryKeySelective(specification);
        //删除从表中的数据并执行添加
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specification.getId());
        specificationOptionMapper.deleteByExample(example);
        //执行增加操作
        List<TbSpecificationOption> list = new ArrayList<>();
        for (int i=0;i<orders.length;i++){
            TbSpecificationOption option = new TbSpecificationOption();
            option.setOptionName(optionName[i]);
            option.setSpecId(specification.getId());
            option.setOrders(orders[i]);
            list.add(option);
        }
        //调用mapper接口增加数据
        specificationOptionMapper.insertInfo(list);
    }

}
